﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rename
{
    // 自定义复合控件：包含静态文本和可编辑文本框
   
    public partial class Form10 : Form
    {
        private MixedControl mixedControl;
        private ListViewItem.ListViewSubItem currentSubItem;
        // 允许混合显示的列索引
        private int[] mixedColumns = { 1 };
        public Form10()
        {
            InitializeComponent();
            InitializeListView();
            InitializeMixedControl();
        }
        private void InitializeListView()
        {
            listView1.View = View.Details;
            listView1.FullRowSelect = true;
            listView1.GridLines = true;

            // 添加列
            listView1.Columns.Add("ID", 80);
            listView1.Columns.Add("库存信息（混合显示）", 250);  // 这一列将包含静态文本和可编辑框
            listView1.Columns.Add("状态", 100);

            // 添加示例数据（格式："静态文本|可编辑值"）
            AddListViewItem("1", "数量:|100", "正常");
            AddListViewItem("2", "数量:|200", "正常");
            AddListViewItem("3", "数量:|50", "低库存");

            // 绑定双击事件
            listView1.MouseDoubleClick += ListView1_MouseDoubleClick;
        }
        private void AddListViewItem(string id, string stockInfo, string status)
        {
            var item = new ListViewItem(id);

            // 解析混合内容并显示为组合文本
            var parts = stockInfo.Split('|');
            item.SubItems.Add($"{parts[0]} {parts[1]}");  // 显示为 "数量: 100"
            item.SubItems.Add(status);

            listView1.Items.Add(item);
        }
        private void InitializeMixedControl()
        {
            mixedControl = new MixedControl
            {
                Visible = false,
                BackColor=Color.Red
            };

            // 绑定编辑完成事件
            mixedControl.EditTextBox.KeyDown += EditTextBox_KeyDown;
            mixedControl.EditTextBox.LostFocus += EditTextBox_LostFocus;

            listView1.Controls.Add(mixedControl);
        }
        private void ListView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            var hitTest = listView1.HitTest(e.X, e.Y);

            if (hitTest.SubItem != null)
            {
                int columnIndex = hitTest.Item.SubItems.IndexOf(hitTest.SubItem);

                // 只在指定列启用混合编辑
                if (Array.IndexOf(mixedColumns, columnIndex) != -1)
                {
                    StartMixedEditing(hitTest.SubItem);
                }
            }
        }
        private void StartMixedEditing(ListViewItem.ListViewSubItem subItem)
        {
            currentSubItem = subItem;

            // 解析子项文本（格式："静态文本 可编辑值"）
            var textParts = subItem.Text.Split(new[] { ' ' }, 2);  // 按第一个空格分割
            if (textParts.Length < 2) return;

            // 设置复合控件
            mixedControl.StaticText = textParts[0];  // 保存静态部分（如"数量:"）
            mixedControl.StaticLabel.Text = textParts[0] + " ";  // 显示静态文本
            mixedControl.EditTextBox.Text = textParts[1];  // 显示可编辑部分
            mixedControl.Bounds = subItem.Bounds;  // 覆盖子项位置
            mixedControl.Visible = true;
            mixedControl.EditTextBox.Focus();
            mixedControl.EditTextBox.SelectAll();
        }
        private void EndMixedEditing()
        {
            if (currentSubItem != null && mixedControl.Visible)
            {
                // 更新子项文本为"静态文本 新值"
                currentSubItem.Text = $"{mixedControl.StaticText} {mixedControl.EditTextBox.Text}";
                mixedControl.Visible = false;
                currentSubItem = null;
            }
        }
        private void EditTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                EndMixedEditing();
                e.SuppressKeyPress = true;
            }
            else if (e.KeyCode == Keys.Escape)
            {
                mixedControl.Visible = false;
                currentSubItem = null;
            }
        }

        private void EditTextBox_LostFocus(object sender, EventArgs e)
        {
            EndMixedEditing();
        }
    }
    public class MixedControl : Control
    {
        public Label StaticLabel { get; } = new Label();
        public TextBox EditTextBox { get; } = new TextBox();
        public string StaticText { get; set; }  // 静态文本部分

        public MixedControl()
        {
            // 设置布局为水平排列
            StaticLabel.Dock = DockStyle.Left;
            EditTextBox.Dock = DockStyle.Fill;
            EditTextBox.BorderStyle = BorderStyle.FixedSingle;

            // 添加控件
            Controls.Add(StaticLabel);
            Controls.Add(EditTextBox);
        }
    }
}
